package org.bpms.config;


import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;
import org.beetl.core.GroupTemplate;
import org.beetl.ext.jfinal.BeetlRenderFactory;
import org.bpms.handler.BpmsHandler;
import org.bpms.interceptor.GlobalInterceptor;
import org.bpms.model.admin.Book;
import org.bpms.model.admin.Booktemp;
import org.bpms.plugins.bjui.SchedulerPlugin;
import org.bpms.routes.AdminRoutes;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.plugin.tablebind.AutoTableBindPlugin;
import com.jfinal.ext.plugin.tablebind.SimpleNameStyles;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.redis.RedisPlugin;

import cn.dreampie.sqlinxml.SqlInXmlPlugin;
import cn.dreampie.sqlinxml.SqlKit;

/**
 * jfinal配置器
 * @author hupei
 *
 */
public class BpmsConfig extends JFinalConfig{
	public static C3p0Plugin c3p0Plugin;
	@Override
	public void configConstant(Constants me) {
		PropKit.use("config.properties");//加载配置文件
		me.setDevMode(PropKit.getBoolean("devMode"));//开发者模式
		//me.setViewType(ViewType.JSP);//以jsp为视图文件
		me.setEncoding("UTF-8");
		//beetl模板
		me.setMainRenderFactory(new BeetlRenderFactory());
		GroupTemplate groupTemplate = BeetlRenderFactory.groupTemplate;
		
		//文件上传最大多少，这里设置300M
		me.setMaxPostSize(300 * 1024 * 1024);
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new BpmsHandler());
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new GlobalInterceptor());
	}

	@Override
	public void configPlugin(Plugins me) {
		Prop p = PropKit.use("config.properties");//非第一次使用该配置文件
		//加载数据库配置
		c3p0Plugin = new C3p0Plugin(p.get("jdbc_url"), 
				p.get("jdbc_username"), p.get("jdbc_password"));
		me.add(c3p0Plugin);
		//自动扫描model，绑定table
		AutoTableBindPlugin arp = new AutoTableBindPlugin(c3p0Plugin , SimpleNameStyles.LOWER);
		me.add(arp);
		
		//sql语句plugin---复杂的sql语句可以用xml文件管理起来
		me.add(new SqlInXmlPlugin("mysql"));
		
	    
	    //简单任务调度--内存持有，无持久化
	    SchedulerPlugin sp = new SchedulerPlugin("job.properties");
	    //添加插件
	    me.add(sp);
	}

	@Override
	public void configRoute(Routes me) {
		me.add(new AdminRoutes());
	}

	@Override
	public void afterJFinalStart() {
		//sqlxmlplugin插件测试
		//System.out.println(SqlKit.sql("user.findInfoBySelect"));
		super.afterJFinalStart();
	}
}
